<?php
/*
 * 体检解析标准化
 */
class reportStandard{
/*
 * @param $arrayItemResult   包含parantName和itemSet的数组
 * @param $table 		数据表，分别为meinian、aikang和ciming
 * @param $dbInsert		是否插入数据库,默认不插入
 */
public function standard($arrayItemResult,$table,$dbInsert=false){

	/*
	 * 体检报告格式统一化
	 */
	$con = mysql_connect("mysql.jiankang.10086.cn:3305","dbuser02","dbuser02_2014");
	if (!$con){
	  die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("standard", $con);
	
	$arr_result_standard=array();
	foreach($arrayItemResult as $key=>$val){
			//把体检大项名称标准化
			$parentName = $val->parentName;			
			$sql_parentName = <<<EOF
				select `programname` from `programinfo` where `$table`= "$parentName"
EOF;
			//var_dump($sql_parentName);continue;
			mysql_query('set names UTF8');
			$result = mysql_query($sql_parentName);
			//var_dump($result);continue;
			$row = mysql_fetch_array($result);
			//var_dump($row);continue;
			if($row){
				$arr_parent=array("parentName"=>$row['programname'],"itemSet"=>array());
			}else{
				$arr_parent=array("parentName"=>$parentName,"itemSet"=>array());
			}
		
			//把体检小项名称标准化
			
			foreach($val->itemSet as $k=>$v){
				$itemName = $v->itemName;
				if($itemName=="其它"){
					$itemName=$parentName."其它";
				}
				$sql_item = <<<EOF
					select `itemname` from `iteminfo` where `$table`= "$itemName"
EOF;
				mysql_query('set names UTF8');
				$result_itemName = mysql_query($sql_item,$con);
				$row = mysql_fetch_array($result_itemName);
				if($row){
					$array_item=array("itemName"=>$row['itemname'],"itemValue"=>$v->itemValue,"unit"=>$v->unit,"normal"=>$v->normal);
				}else{
					@$array_item=array("itemName"=>$itemName,"itemValue"=>$v->itemValue,"unit"=>$v->unit,"normal"=>$v->normal);
				}
				array_push($arr_parent['itemSet'],$array_item);
				
			}
			
			array_push($arr_result_standard, $arr_parent);
	}
	mysql_close($con);
	//var_dump($arr_result_standard);exit();
	$result = $arr_result_standard;
	return $result;		//
	/*
	if($dbInsert==false){
		$result = json_encode($arr_result_standard,JSON_UNESCAPED_UNICODE);
		return $result;		//如果体检报告不插入数据库，直接返回数据
	}else{					
		$con = mysql_connect("localhost","root","");
		if (!$con){
		  die('Could not connect: ' . mysql_error());
		}
		mysql_select_db("report", $con);
		foreach($data as $key=>$val){
			foreach ($val as $k=>$v){
				$sql_insert = <<<EOF
					INSERT INTO  `report`.`iteminfo` (`itemid` ,`reportid` ,`programid` ,`userid` ,`itemname` ,`result` ,`unit` ,`normal` ,`issue`)
VALUES (
'',  '0',  '0',  '0',  $v['itemname'],  $v['result'],  $v['unit'],  $v['normal'],  $v['issue']
);
EOF;
var_dump($sql_insert);exit();
			}
		}
		var_dump($data);exit();
	}
	*/
	
}

	/*
	 * 把体检大项目名称录入programname表中
	 * @$dataItems 包含parentName和itemName的数组
	 * @field  需要更新的字段。可以为meinian、ciming和aikang
	 * return array()  只包含parentName的数组
	 */
	public function editParentNameArr($field,$dataItems){
		//var_dump($dataItems);exit();
		//链接数据库
		$con = mysql_connect("localhost","root","");
		if (!$con){
	 	 die('Could not connect: ' . mysql_error());
		}
		mysql_select_db("standard", $con);
		
		//从数据库中获取体检大项目名称，组成数组
		$sql_select =<<<EOF
			select `programname` from `programinfo`
EOF;
		mysql_query('set names UTF8');
		$result = mysql_query($sql_select,$con);
		while($row = mysql_fetch_array($result)){
			$data[]=$row['programname'];
		}
		$dataItems = $this->getParentNamesArr($dataItems);
		
		//获取体检报告解析得到的体检大项名称，进行比较、入库。
		foreach ($data as $key=>$val){
			foreach ($dataItems as $k=>$v){
				
					$sql_update=<<<EOF
					update  `programinfo` set `$field` ="$val" where `programname` like "%$v%"
EOF;
//var_dump($sql_update);continue;
					mysql_query('set names UTF8');
					$result = mysql_query($sql_update);
				//var_dump($result);
			}
		}
}
	/*
	 * 获取体检大项
	 */
	public function getParentNamesArr($data){
		$parentNameArr=array();
		foreach($data as $key=>$val){
			array_push($parentNameArr, $val['parentName']);
		}
		return $parentNameArr;
	}

}